package com.mvd.app.commons;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.mvd.app.api.MyTypeAdapterFactory;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

/**
 * @author liuxj
 * @ClassName: JsonParseUtil
 * @Description: json解析工具类
 * @date 2014-10-15 上午11:38:34
 */
public class JsonUtil {
    private JsonUtil() {
    }

    /**
     * 对象转换成json字符串
     *
     * @param obj
     * @return
     */
    public static String toJson(Object obj) {
//		Gson gson = new Gson();
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.registerTypeAdapterFactory(new MyTypeAdapterFactory()).create();
        return gson.toJson(obj);
    }

    /**
     * json字符串转成对象
     *
     * @param str
     * @param type
     * @return
     */
    public static <T> T fromJson(String str, Type type) {
        Gson gson = new Gson();
        return gson.fromJson(str, type);
    }

    /**
     * json字符串转成对象
     *
     * @param str
     * @param type
     * @return
     */
    public static <T> T fromJson(String str, Class<T> type) {
//		Gson gson = new Gson();
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.registerTypeAdapterFactory(new MyTypeAdapterFactory()).create();
        return gson.fromJson(str, type);
    }

    /**
     * 报异常： com.google.gson.internal.StringMap cannot be cast to com
     *
     * @param jsonstr
     * @param class1
     * @return
     */
    public static <T> List<T> getJsonToModelList(String jsonstr, Class<T> class1) {

        Type listType = new TypeToken<List<T>>() {
        }.getType();
        Gson gson = new Gson();
        List<T> linkedList = gson.fromJson(jsonstr, listType);
        return linkedList;
    }

    /**
     * 解析数组
     *
     * @param s
     * @param clazz
     * @return
     */
    public static <T> List<T> stringToArray(String s, Class<T[]> clazz) {
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.registerTypeAdapterFactory(new MyTypeAdapterFactory()).create();

//		T[] arr = new Gson().fromJson(s, clazz);
        T[] arr = gson.fromJson(s, clazz);
        return Arrays.asList(arr);
    }

    public static boolean isJson(String jsonStr) {
//		try {
//			Gson gson = new Gson();
//			gson.fromJson(jsonStr, Object.class);
//			return true;
//		} catch (JsonSyntaxException e) {
//			return false;
//		}

        try {
            // 尝试将字符串解析为 JSON 元素
            JsonElement jsonElement = new JsonParser().parse(jsonStr);
            return true;
        } catch (JsonSyntaxException e) {
            // 解析过程中出现异常，说明不是合法的 JSON 字符串
            return false;
        }
    }

    public static void main(String[] args) {
        String validJson = "{\"key\": \"value\"}";
        String invalidJson = "{key: value}";
        String result = "互联网预约信息下载失败：未实现该方法或操作。";
        System.out.println(isJson(validJson) + "111111111");
        System.out.println(isJson(invalidJson) + "22222222");
        System.out.println(isJson(result) + "3333333333");
    }

}
